﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace mapleHttpTestServer
{
    public class Fish
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Pinyin { get; set; }
        public string Describe { get; set; }
        public bool IsLivingInSea { get; set; } = false;
    }

    public class FishDataset
    {
        private static IList<Fish> database = new List<Fish>();

        static FishDataset()
        {
            database.Add(new Fish()
            {
                Id = 9527,
                Name = "青鱼",
                Pinyin = "qinyu",
                Describe = "青鱼（Mylopharyngodon piceus）又叫黑鲩，全身有较大的鳞片。外形很像草鱼，但全身的鳞片和鱼鳍都带灰黑色。青鱼的名字也由此而来"
            });

            database.Add(new Fish()
            {
                Id = 9528,
                Name = "草鱼",
                Pinyin = "caoyu",
                Describe = "草鱼（Grass carp）又称鲩、油鲩、草鲩、白鲩、草鱼、草根（东北）、混子、黑青鱼等"
            });

            database.Add(new Fish()
            {
                Id = 9529,
                Name = "鲢鱼",
                Pinyin = "lianyu",
                Describe = "鲢鱼（Hypophthalmichthysmolitrix），又叫白鲢、鳊鱼、水鲢、跳鲢、鲢子，属于鲤形目，鲤科，是著名的四大家鱼之一"
            });

            database.Add(new Fish()
            {
                Id = 9530,
                Name = "鳙鱼",
                Pinyin = "yongyu",
                Describe = "鳙鱼（Aristichthys nobilis （Richardson））又叫花鲢、黄鲢、鱃鱼、胖头鱼、包头鱼、大头鱼、黑鲢、麻鲢、也有叫雄鱼"
            });

            database.Add(new Fish()
            {
                Id = 9531,
                Name = "多宝鱼",
                Pinyin = "duobaoyu",
                Describe = "多宝鱼是大菱鲆(Scophthalmus maximus) 的英译名兼商品名，又叫欧洲比目鱼，英文名Turbot。 大菱鲆属于鲽形目、鲆科、菱鲆属，主要产区位于大西洋东侧沿岸，是东北大西洋沿岸的特有名贵低温经济鱼种之一",
                IsLivingInSea = true
            });

        }

        public IList<Fish> List()
        {
            return database;
        }

        public Fish Get(long id)
        {
            return database.FirstOrDefault(f => f.Id == id);
        }

        public void Post(Fish fish)
        {
            if (fish == null)
            {
                Console.WriteLine("WARN: Post Fish Is Null , Post OP Canceled . ");
                return;
            }

            if (database.Count(f => f.Id == fish.Id) > 0)
            {
                Console.WriteLine("WARN: Post Fish Already Exists, Post OP Canceled . ");
                return;
            }

            database.Add(fish);
        }


        public void Put(long id, Fish fish)
        {
            if (fish == null)
            {
                Console.WriteLine("WARN: Post Fish Is Null , Put OP Canceled . ");
                return;
            }

            var tmp = database.FirstOrDefault(f => f.Id == id);

            if (tmp == null)
            {
                Console.WriteLine("WARN: Post Fish Not Exists, Put OP Canceled . ");
                return;
            }

            tmp.Name = fish.Name;
            tmp.Pinyin = fish.Pinyin;
            tmp.Describe = fish.Describe;
        }

        public void Delete(long id)
        {
            var tmp = database.FirstOrDefault(f => f.Id == id);
            if (tmp == null)
            {
                Console.WriteLine("WARN: Post Fish Not Exists, Delete OP Canceled . ");
                return;
            }

            database.Remove(tmp);
        }
    }


}
